package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/model"
	"shop-common/library/serializer"
	"shop-common/library/variables"
	"shop-common/utils/contextArgs"
)

type SettingCityController struct {
}

func (s *SettingCityController) List(ctx *gin.Context) {
	var list []*model.SettingCity
	err := variables.GormDB().Model(&model.SettingCity{}).
		Where("is_show = 1").Find(&list).Error
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", list)
}

func (s *SettingCityController) ListByPid(ctx *gin.Context) {
	pid, err := contextArgs.GetUriIntQuery(ctx, "pid")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}
	var list []*model.SettingCity
	err = variables.GormDB().Model(&model.SettingCity{}).
		Where("is_show = 1").Where("parent_id = ?", pid).Find(&list).Error
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", list)
}

